package com.exsun.ex1077.ui.activity.msg

import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONObject
import com.drake.brv.PageRefreshLayout
import com.exsun.ex1077.network.apiService
import com.exsun.ex1077.network.entity.Msg
import com.exsun.ex1077.network.entity.MsgSend
import com.exsun.ex1077.network.entity.Page
import com.zhangping.fastjetpack.base.BaseViewModel
import com.zhangping.fastjetpack.extend.base.ResultState
import com.zhangping.fastjetpack.extend.base.request
import com.zhangping.fastjetpack.extend.databind.BooleanObservableField

/**
 * 描述：
 * 作者：ZhangPing
 * 创建时间：2022/8/9 11:24
 */
class MsgViewModel: BaseViewModel() {

    //页面数量
    val pageSize = 20
    //当前是否是选择模式
    var selectMode = BooleanObservableField()

    val listResult = MutableLiveData<ResultState<Page<Msg>?>>()

    fun loadData(pageIndex: Int = PageRefreshLayout.startIndex){
        request({ apiService.msgList(JSONObject().apply {
            put("pageIndex", pageIndex)
            put("pageSize", pageSize)
        })}, listResult)
    }

    fun loadDetail(id: String): MutableLiveData<MsgSend?>{
        val liveData = MutableLiveData<MsgSend?>()
        request({ apiService.msgDetail(id)}, { liveData.value = it })
        return liveData
    }

    fun removeMsg(id: String): MutableLiveData<Boolean> {
        val liveData = MutableLiveData<Boolean>()
        request({ apiService.msgRemove(id)}, { liveData.value = true })
        return liveData
    }

    fun batchRemoveMsg(ids: String): MutableLiveData<Boolean> {
        val liveData = MutableLiveData<Boolean>()
        request({ apiService.msgBatchRemove(ids)}, { liveData.value = true })
        return liveData
    }
}